package com.hyt.it.ogt.pj.service.impl;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.collections4.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONObject;
import com.hyt.common.apiInfor.ApiResponse;
import com.hyt.core.base.BaseServiceImpl;
import com.hyt.it.ogt.data.model.vo.StudentAnswerVO;
import com.hyt.it.ogt.pj.common.MarkException;
import com.hyt.it.ogt.pj.common.ResponseCode;
import com.hyt.it.ogt.pj.feign.KwClient;
import com.hyt.it.ogt.pj.mapper.StudentAnswerMapper;
import com.hyt.it.ogt.pj.mapper.StudentMapper;
import com.hyt.it.ogt.pj.model.entity.Student;
import com.hyt.it.ogt.pj.model.entity.StudentAnswer;
import com.hyt.it.ogt.pj.model.vo.StudentAnswerVo;
import com.hyt.it.ogt.pj.service.IStudentAnswerService;
import com.hyt.it.ogt.ykcj.domain.vo.exchange.CandidateAnswersVO;
import com.hyt.it.ogt.ykcj.feign.model.CandidateAnswersResultEntity;

/**
 * <p>
 * 考生作答表 服务实现类
 * </p>
 *
 * @author qiaozj
 * @since 2020-07-09
 */
@Service
public class StudentAnswerServiceImpl extends BaseServiceImpl<StudentAnswerMapper, StudentAnswer> implements IStudentAnswerService {

	private final Logger logger = LoggerFactory.getLogger(StudentAnswerServiceImpl.class);

    @Resource
    private KwClient kwClient;

    @Resource
    private StudentAnswerMapper studentAnswerMapper;
    
    @Resource
    private StudentMapper studentMapper;

    /**
     * 初始化考生答案
     * @param projectId 评卷项目id
     * @param admission 准考证号
     */
    @Override
    public List<StudentAnswerVo> initStudentAnswer(String projectId, String admission) {
        // 用户是否传准考证号来区分是否为增量添加评卷任务的初始化
        ApiResponse<List<StudentAnswerVo>> res = kwClient.getStudentAnswer(projectId, admission);
        logger.info(">>>拉取到的评卷学生答案数据:{}" , JSONObject.toJSONString(res));
        List<StudentAnswerVo> studentAnswerVos = res.getData();
        if(CollectionUtils.isNotEmpty(studentAnswerVos)){
            List<StudentAnswer> studentAnswers = new ArrayList<>();
            for(StudentAnswerVo studentAnswerVo: studentAnswerVos){
                StudentAnswer studentAnswer = new StudentAnswer();
                studentAnswer.setProjectId(studentAnswerVo.getProjectId());
                studentAnswer.setSubjectId(studentAnswerVo.getSubjectId());
                studentAnswer.setItemId(studentAnswerVo.getItemId());
                studentAnswer.setAdmissionNum(studentAnswerVo.getAdmissionNumber());
                studentAnswer.setAnswer(studentAnswerVo.getSubjectiveAnswer());
                studentAnswers.add(studentAnswer);
            }
            this.saveBatch(studentAnswers);
        }else{
            throw new MarkException(ResponseCode.INIT_PROJECT_NO_STUDENT_ANSWER.toString());
        }
		return studentAnswerVos;
    }


    /**
     * 根据评卷项目id查询所有考生答案
     * @param projectId 评卷项目id
     * @return 返回考生答案list
     */
    @Override
    public List<StudentAnswer> selectByProject (String projectId){
        return studentAnswerMapper.selectByProject(projectId);
    }

    @Override
    public void saveStudentAnswer(String projectId, List<StudentAnswerVO> studentAnswerVOs) {
        List<StudentAnswer> studentAnswers = new ArrayList<>();
        for(StudentAnswerVO studentAnswerVO: studentAnswerVOs){
            StudentAnswer studentAnswer = new StudentAnswer();
            studentAnswer.setProjectId(studentAnswerVO.getProjectId());
            studentAnswer.setSubjectId(studentAnswerVO.getSubjectId());
            studentAnswer.setItemId(studentAnswerVO.getItemId());
            studentAnswer.setAdmissionNum(studentAnswerVO.getAdmissionNumber());
            studentAnswer.setAnswer(studentAnswerVO.getSubjectiveAnswer());
            studentAnswers.add(studentAnswer);
        }
        this.saveBatch(studentAnswers);
    }


    /**
     * @see com.hyt.it.ogt.pj.service.IStudentAnswerService#getInfoByProjectIdAndStudentId(java.lang.String, java.lang.String)
     **/
    @Override
    public CandidateAnswersResultEntity queryCandidateAnswers(String projectId, String studentId) {
        Student student = studentMapper.selectById(studentId);
        CandidateAnswersResultEntity candidateAnswersResultEntity = new CandidateAnswersResultEntity();
        CandidateAnswersVO data = new CandidateAnswersVO ();
        data.setPaperId(student.getPaperId());
        
        candidateAnswersResultEntity.setData(data);
        return candidateAnswersResultEntity;
    }
}
